<template>
  <div class="login-container">
    <div class="left">
      <div class="title-box">
        <img src="@/assets/img/login_icon.png" alt="" />
        <span class="title">白马面面</span>
        <span class="line"></span>
        <span class="sub-title">用户登录</span>
      </div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="login-form"
      >
        <el-form-item prop="phone" placeholder="请输入手机号">
          <el-input
            v-model="ruleForm.phone"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password" placeholder="请输入密码">
          <el-input
            v-model="ruleForm.password"
            prefix-icon="el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>

        <el-row>
          <el-col :span="16">
            <el-form-item prop="code" placeholder="请输入验证码">
              <el-input
                v-model="ruleForm.code"
                prefix-icon="el-icon-key"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="7"
            ><img
              :src="loginCaptcha"
              alt=""
              style="width:100%;height:40px;margin-left:10px"
              @click="changeloginCaptcha"
          /></el-col>
        </el-row>
        <el-form-item prop="checked">
          <el-checkbox v-model="ruleForm.checked"></el-checkbox
          >&nbsp;我已阅读并同意&nbsp;<el-link
            type="primary"
            target="__blank"
            href="https://in.m.jd.com/help/app/register_info.html"
            >用户协议</el-link
          >和&nbsp;
          <el-link type="primary">隐私条款</el-link>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%;" @click="login"
            >登录</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%;" @click="register"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="right">
      <img src="@/assets/img/login_bg.png" alt="" />
    </div>
    <register ref="registerRef"></register>
  </div>
</template>

<script>
import { saveToken } from '@/utils/token'
import register from '@/views/login/register'
export default {
  components: {
    register
  },
  data () {
    return {
      loginCaptcha: process.env.VUE_APP_BASURL + `/captcha?type=login`,
      ruleForm: {
        phone: '',
        password: '',
        code: '',
        checked: false
      },
      rules: {
        phone: [
          {
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error('手机号码不能为空！'))
              }
              if (!/^1[3456789][0-9]{9}$/.test(value)) {
                return callback(new Error('手机号码不合法'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
          { min: 6, max: 16, message: '密码必须为6-16位', trigger: 'blur' }
        ],
        code: [{ required: true, message: '验证码不能为空', trigger: 'blur' }],
        checked: [
          {
            validator: (tule, value, callback) => {
              value ? callback() : callback(new Error('用户协议必须选择'))
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    changeloginCaptcha () {
      // console.log('123');
      this.loginCaptcha =
        process.env.VUE_APP_BASURL + `/captcha?type=login&t=${new Date() - 0}`
    },
    login () {
      // 全部校验
      this.$refs.ruleForm.validate(async valid => {
        // 如果校验不通过，阻止代码继续往下执行
        if (!valid) return

        const res = await this.$axios.post('/login', this.ruleForm)

        if (res.code === 200) {
          // 提示
          this.$message({
            message:'登录成功！',
            type:'success',
            duration:1000
          })

          // 保存
          saveToken(res.data.token)

          // 跳转
          this.$router.push('/layout')
        } else {
          // this.$message.error(res.message)
          this.$message({
            message: res.message,
            type: 'error',
            duration: 1000
          })
        }
      })
    },
    register () {
      this.$refs.registerRef.dialogVisible = true
    }
  }
}
</script>

<style lang="less">
.login-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: linear-gradient(225deg, #1493fa, #01c6fa);
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 48px;
    .title-box {
      display: flex;
      align-items: center;
      img {
        width: 22px;
        height: 17px;
        margin-right: 16px;
      }
      .title {
        font-size: 24px;
        margin-right: 14px;
      }
      .line {
        width: 1px;
        background-color: #c7c7c7;
        height: 27px;
        margin-right: 12px;
      }
      .sub-title {
        font-size: 22px;
      }
    }
    .login-form {
      margin-top: 29px;
    }
  }
}
</style>
